//
//  AYStatusRetweetView.swift
//  iOSSinaWeb
//
//  Created by yws on 16/5/21.
//  Copyright © 2016年 yws. All rights reserved.
//

import UIKit

class AYStatusRetweetView: UIView {
    
    
    ///被转发的模型数据
    var retweetStatus: AYStatus? {
        didSet{
            if retweetStatus == nil {
                retweetContentLabel.text = nil
            } else {
                let name = retweetStatus?.user?.screen_name ?? "未知姓名"
                let text = retweetStatus?.text ?? ""
                retweetContentLabel.text = "@\(name):\(text)"
            }
            
            retweetPictureView.pictureURLs = retweetStatus?.pictureURLs
            
            retweetContentLabel.snp_updateConstraints { (make) in
                let margin = retweetStatus == nil ? 0 : CellMargin
                
                make.top.equalTo(self).offset(margin)
            }
            
            retweetPictureView.snp_updateConstraints { (make) in
                var margin:CGFloat = CellMargin
                
                if retweetStatus == nil {
                    margin = 0
                } else if retweetStatus?.pictureURLs == nil || retweetPictureView.pictureURLs?.count == 0 {
                    margin = 0
                }
                
                make.top.equalTo(retweetContentLabel.snp_bottom).offset(margin)
            }
            
            self.snp_updateConstraints { (make) in
                let margin = retweetStatus == nil ? 0 : CellMargin
                make.bottom.equalTo(retweetPictureView).offset(margin)
            
            }
            
        }
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        ///设置约束
        retweetContentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(CellMargin)
            make.leading.equalTo(self).offset(CellMargin)
        }
        
        
        retweetPictureView.snp_makeConstraints { (make) in
            make.leading.equalTo(retweetContentLabel)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(CellMargin)
            make.size.equalTo(200)
            
        }
        
        self.snp_makeConstraints { (make) in
            make.bottom.equalTo(retweetPictureView).offset(CellMargin)
        }
        
    }
    
    
    
    
    
    
    // MARK: - lazy
    
    ///图片View
    private lazy var retweetPictureView: AYStatusPictureView = AYStatusPictureView()
    
    ///label
    private lazy var retweetContentLabel: UILabel = {
       let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(CellStatusFontSize)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - CellMargin * 2
        
        
        label.sizeToFit()
        return label
    }()
}
